﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smartec.Accounts.Camel.AMS.Model;
using Smartec.Accounts.Camel.AMS.Model.Facades;
using Smartec.Accounts.Camel.AMS.Model.Entities;
using Smartec.Accounts.Camel.AMS.Model.Managers;
using Smartec.Accounts.Camel.AMS.WebApp.Models;
using System.Text;
using System.Web.Security;

namespace Smartec.Accounts.Camel.AMS.WebApp.Controllers
{
    public class AdminController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            //Membership.CreateUser("admin@cipegypt.com", "123admin");
         // MembershipUser user=  Membership.GetUser("admin@cipegypt.com");
            return View();
        }

       
        public ActionResult RenderForiegnPartialView(string id)
        {
            //PartialView$ForiegnFieldData+Model$+MasterTable$Departments+PropertyName$Department_ID+IsRequired$True
            foreach (string item in id.Split('!'))
            {
                if (!string.IsNullOrEmpty(item))
                {
                    string[] kv = item.Split('$');
                    ViewData[kv[0]] = kv[1];
                }
            }
            //"~/Views/Shared/EditorTemplates/ForiegnFieldData.ascx"
            return PartialView(string.Format("~/Views/Shared/EditorTemplates/{0}.ascx", ViewData["PartialView"]), ViewData["Model"]);
        }

        public ActionResult RenderCascadingField(string id)
        {
            foreach (string item in id.Split('$'))
            {
                if (!string.IsNullOrEmpty(item))
                {
                    string[] kv = item.Split('!');
                    ViewData[kv[0]] = kv[1];
                }
            }
            return PartialView(string.Format("~/Views/Shared/{0}.ascx", ViewData["PartialView"]));
        }

        public ActionResult RenderEmptyView()
        {
            return Content("");
        }

        public ActionResult GetListItems(string id)
        {
            string[] parameters = id.Split('$');
            string entityName = parameters[0];
            string parameterName = parameters[1];
            string parameterValue="";

            bool excludeCurrentLocation = false;
            bool isWorkshopLocations = false;

            if (parameters.Length == 4)
            {
                excludeCurrentLocation = true;
                parameterValue = parameters[3];
                if (parameters[2].ToLower() == "workshop")
                    isWorkshopLocations = true;
            }
            else
            {
                parameterValue = parameters[2];
            }

            var allListItems = AMSManagerFacade.GetListItems(entityName, parameterName, parameterValue);
            
            

            var listItems = allListItems.Select(o => new SelectListItem()
            {
                Text = o.Value,
                Value = o.Key
            });
            
            return Json(listItems);
        }

        public ActionResult GetRelationDetails(string id)
        {
            string[] parameters = id.Split('$');
            string entityName = parameters[0];
            string parameterName = parameters[1];
            string parameterValue = parameters[2];

            string[] detailsItems=AMSManagerFacade.GetListItems(entityName, parameterName, parameterValue).Select(o => o.Value).ToArray();
            string result = string.Join(", ", detailsItems);
            result += string.Format("<br/><a href='/{0}/?{1}={2}' >View full details</a>", entityName, parameterName, parameterValue);
            return Content(result);
        }

     
    }
}
